﻿/* ===============================================================================  
 * Copyright (c) 2010 Acid Frameworks
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * =============================================================================== */

using System;
using System.Collections.Generic;
using Acid.Graphics;
using Acid.Math;

namespace Acid.Rendering
{
    /// <summary>
    /// A Material is a stateful object used to pass parameters to Effects. Primitives reference Materials during rendering.
    /// </summary>
    public class Material : IPackObject, IDisposable
    {
        private static int Count { get; set; }

        /// <summary>
        /// This materials Effect
        /// </summary>
        public Effect Effect                         { get; set; }


        /// <summary>
        /// A Dictionary of Effect Parameters.
        /// </summary>
        public Dictionary<string, object> Parameters { get; protected set; }

        public Material()
        {
            this.ID         = "material_" + Material.Count.ToString(); Material.Count++;
            this.Effect     = null;
            this.Parameters = new Dictionary<string, object>();
        }

        #region IGraphObject Members
        
        /// <summary>
        /// This Materials ID
        /// </summary>
        public string ID   { get; set; }
        
        /// <summary>
        /// This Materials Name
        /// </summary>
        public string Name { get; set; }
        
        #endregion

        #region Methods

        /// <summary>
        /// Binds Material Shader parameters.
        /// </summary>
        public void Set()
        {
            foreach (KeyValuePair<string, object> kv in this.Parameters)
            {
                if(kv.Value is int)
                    this.Effect.Shader.Parameter(kv.Key, (int)kv.Value);
                if (kv.Value is float)
                    this.Effect.Shader.Parameter(kv.Key, (float)kv.Value);
                if (kv.Value is double)
                    this.Effect.Shader.Parameter(kv.Key, (double)kv.Value);
                if (kv.Value is Vector2)
                    this.Effect.Shader.Parameter(kv.Key, (Vector2)kv.Value);
                if (kv.Value is Vector3)
                    this.Effect.Shader.Parameter(kv.Key, (Vector3)kv.Value);
                if (kv.Value is Vector4)
                    this.Effect.Shader.Parameter(kv.Key, (Vector4)kv.Value);
                if (kv.Value is Matrix)
                    this.Effect.Shader.Parameter(kv.Key, (Matrix)kv.Value);
                if (kv.Value is TextureBase)
                    this.Effect.Shader.Parameter(kv.Key, (TextureBase)kv.Value);
                if (kv.Value is int[])
                    this.Effect.Shader.Parameter(kv.Key, (int[])kv.Value);
                if (kv.Value is float[])
                    this.Effect.Shader.Parameter(kv.Key, (float[])kv.Value);
                if (kv.Value is double[])
                    this.Effect.Shader.Parameter(kv.Key, (double[])kv.Value);
                if (kv.Value is Vector2[])
                    this.Effect.Shader.Parameter(kv.Key, (Vector2[])kv.Value);
                if (kv.Value is Vector3[])
                    this.Effect.Shader.Parameter(kv.Key, (Vector3[])kv.Value);
                if (kv.Value is Vector4[])
                    this.Effect.Shader.Parameter(kv.Key, (Vector4[])kv.Value);
                if (kv.Value is Matrix[])
                    this.Effect.Shader.Parameter(kv.Key, (Matrix[])kv.Value);
                if (kv.Value is TextureBase[])
                    this.Effect.Shader.Parameter(kv.Key, (TextureBase[])kv.Value);
            }
        }
        
        #endregion

        #region IDisposable Members

        private bool disposed = false;

        private void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    if (this.Effect != null)
                    {
                        this.Effect.Dispose();
                    }
                }

                this.disposed = true;
            }
        }

        public void Dispose()
        {
            this.Dispose(true);
        }

        ~Material()
        {
            this.Dispose(false);
        }

        #endregion
    }
}
